from rest_framework import serializers
from django_redis import get_redis_connection
from redis.exceptions import RedisError
import re
import logging

from .models import User
logger = logging.getLogger('django')

class CreateUserSerializer(serializers.ModelSerializer):
    '''创建用户序列化器'''
    password2=serializers.CharField(label='确认密码',required=True,allow_null=False,allow_blank=False,write_only=True)
    sms_code = serializers.CharField(label='短信验证码',required=True,allow_null=False,allow_blank=False,write_only=True)
    allow = serializers.CharField(label='同意协议',required=True,allow_null=False,allow_blank=False,write_only=True)
    token = serializers.CharField(label='登录状态token',read_only=True)

    def validate_mobile(self, value):
        '''验证手机号'''
        if not re.match(r'^1[345789]\d{9}$',value):
            raise serializers.ValidationError('手机号格式错误')
        return value

    def validate_allow(self, value):
        '''检验用户是否同一协议'''
        if value !='True':
            raise serializers.ValidationError('请同意用户协议')
        return value

    def validate(self, data):
        '''判断两次密码'''
        if data['password']!=data['password2']:
            raise serializers.ValidationError('两次密码不一致')
























